<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="membersId">
        <el-input
          v-model="queryParams.membersId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店ID" prop="storeId">
        <el-input
          v-model="queryParams.storeId"
          placeholder="请输入门店ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="收货地址信息" prop="address">-->
<!--        <el-input-->
<!--          v-model="queryParams.address"-->
<!--          placeholder="请输入收货地址信息"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="使用的优惠券ID" prop="couponId">-->
<!--        <el-input-->
<!--          v-model="queryParams.couponId"-->
<!--          placeholder="请输入使用的优惠券ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="商品总金额" prop="itemsAmount">-->
<!--        <el-input-->
<!--          v-model="queryParams.itemsAmount"-->
<!--          placeholder="请输入商品总金额"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="平台活动优惠金额" prop="discountAmount">-->
<!--        <el-input-->
<!--          v-model="queryParams.discountAmount"-->
<!--          placeholder="请输入平台活动优惠金额"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="优惠券减免金额" prop="couponAmount">-->
<!--        <el-input-->
<!--          v-model="queryParams.couponAmount"-->
<!--          placeholder="请输入优惠券减免金额"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="运费" prop="shippingFee">-->
<!--        <el-input-->
<!--          v-model="queryParams.shippingFee"-->
<!--          placeholder="请输入运费"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="应付款总金额" prop="totalAmount">-->
<!--        <el-input-->
<!--          v-model="queryParams.totalAmount"-->
<!--          placeholder="请输入应付款总金额"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable style="width: 200px" >
          <el-option
            v-for="dict in order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="paymentType">
        <el-select v-model="queryParams.paymentType" placeholder="请选择支付方式" clearable style="width: 200px" >
          <el-option
            v-for="dict in pay"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="付款时间" prop="paymentTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.paymentTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择付款时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="期望送达时间" prop="expectDeliveryTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.expectDeliveryTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择期望送达时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="实际送达时间" prop="actualDeliveryTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.actualDeliveryTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择实际送达时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="订单备注" prop="notes">-->
<!--        <el-input-->
<!--          v-model="queryParams.notes"-->
<!--          placeholder="请输入订单备注"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="配送方式" prop="deliveryMethod">
        <el-select v-model="queryParams.deliveryMethod" placeholder="请选择配送方式" clearable style="width: 200px" >
          <el-option
            v-for="dict in delivery_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="自提码" prop="pickupCode">
        <el-input
          v-model="queryParams.pickupCode"
          placeholder="请输入自提码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="自提状态" prop="pickupStatus">
        <el-select v-model="queryParams.pickupStatus" placeholder="请选择自提状态" clearable style="width: 200px" >
          <el-option
            v-for="dict in pickup_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="实际提货时间" prop="pickupTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.pickupTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择实际提货时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="自提备注信息" prop="pickupNotes">-->
<!--        <el-input-->
<!--          v-model="queryParams.pickupNotes"-->
<!--          placeholder="请输入自提备注信息"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['transaction:shoporder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['transaction:shoporder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['transaction:shoporder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['transaction:shoporder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shoporderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单ID，主键自增" align="center" prop="id" />
      <el-table-column label="订单编号，唯一" align="center" prop="orderNo" />
<!--      <el-table-column label="用户ID" align="center" prop="membersId" />-->
<!--      <el-table-column label="门店ID" align="center" prop="storeId" />-->
      <el-table-column label="用户" align="center">
        <template #default="scope">
          {{ scope.row.membersId }} - {{ scope.row.members ? scope.row.members.nickname : '' }}
        </template>
      </el-table-column>
      <el-table-column label="门店" align="center">
        <template #default="scope">
          {{ scope.row.storeId }} - {{ scope.row.storeInfo ? scope.row.storeInfo.name : '' }}
        </template>
      </el-table-column>
      <el-table-column label="收货地址信息" align="center" prop="address" />
<!--      <el-table-column label="使用的优惠券ID" align="center" prop="couponId" />-->
      <el-table-column label="使用的优惠券" align="center">
        <template #default="scope">
          {{ scope.row.couponId }} - {{ scope.row.couponInfo ? scope.row.couponInfo.name : '' }}
        </template>
      </el-table-column>
      <el-table-column label="商品总金额" align="center" prop="itemsAmount" />
      <el-table-column label="平台活动优惠金额" align="center" prop="discountAmount" />
      <el-table-column label="优惠券减免金额" align="center" prop="couponAmount" />
      <el-table-column label="运费" align="center" prop="shippingFee" />
      <el-table-column label="应付款总金额" align="center" prop="totalAmount" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="paymentType">
        <template #default="scope">
          <dict-tag :options="pay" :value="scope.row.paymentType"/>
        </template>
      </el-table-column>
      <el-table-column label="付款时间" align="center" prop="paymentTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.paymentTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="期望送达时间" align="center" prop="expectDeliveryTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expectDeliveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际送达时间" align="center" prop="actualDeliveryTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.actualDeliveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单备注" align="center" prop="notes" />
      <el-table-column label="配送方式" align="center" prop="deliveryMethod">
        <template #default="scope">
          <dict-tag :options="delivery_method" :value="scope.row.deliveryMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="自提码，用于核销取货" align="center" prop="pickupCode" />
      <el-table-column label="自提状态" align="center" prop="pickupStatus">
        <template #default="scope">
          <dict-tag :options="pickup_status" :value="scope.row.pickupStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="实际提货时间" align="center" prop="pickupTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.pickupTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="自提备注信息" align="center" prop="pickupNotes" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['transaction:shoporder:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['transaction:shoporder:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单表对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="shoporderRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号，唯一" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号，唯一" />
        </el-form-item>
        <el-form-item label="用户ID" prop="membersId">
          <el-input v-model="form.membersId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="门店ID" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入门店ID" />
        </el-form-item>
        <el-form-item label="收货地址信息" prop="address">
          <el-input v-model="form.address" placeholder="请输入收货地址信息" />
        </el-form-item>
        <el-form-item label="使用的优惠券ID" prop="couponId">
          <el-input v-model="form.couponId" placeholder="请输入使用的优惠券ID" />
        </el-form-item>
        <el-form-item label="商品总金额" prop="itemsAmount">
          <el-input v-model="form.itemsAmount" placeholder="请输入商品总金额" />
        </el-form-item>
        <el-form-item label="平台活动优惠金额" prop="discountAmount">
          <el-input v-model="form.discountAmount" placeholder="请输入平台活动优惠金额" />
        </el-form-item>
        <el-form-item label="优惠券减免金额" prop="couponAmount">
          <el-input v-model="form.couponAmount" placeholder="请输入优惠券减免金额" />
        </el-form-item>
        <el-form-item label="运费" prop="shippingFee">
          <el-input v-model="form.shippingFee" placeholder="请输入运费" />
        </el-form-item>
        <el-form-item label="应付款总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入应付款总金额" />
        </el-form-item>
        <el-form-item label="订单状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择订单状态">
            <el-option
              v-for="dict in order_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentType">
          <el-radio-group v-model="form.paymentType">
            <el-radio
              v-for="dict in pay"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="付款时间" prop="paymentTime">
          <el-date-picker clearable
            v-model="form.paymentTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择付款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="期望送达时间" prop="expectDeliveryTime">
          <el-date-picker clearable
            v-model="form.expectDeliveryTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择期望送达时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际送达时间" prop="actualDeliveryTime">
          <el-date-picker clearable
            v-model="form.actualDeliveryTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择实际送达时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单备注" prop="notes">
          <el-input v-model="form.notes" placeholder="请输入订单备注" />
        </el-form-item>
        <el-form-item label="逻辑删除" prop="delFlag">
          <el-select v-model="form.delFlag" placeholder="请选择逻辑删除">
            <el-option
              v-for="dict in del_flag"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配送方式" prop="deliveryMethod">
          <el-select v-model="form.deliveryMethod" placeholder="请选择配送方式">
            <el-option
              v-for="dict in delivery_method"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="自提码，用于核销取货" prop="pickupCode">
          <el-input v-model="form.pickupCode" placeholder="请输入自提码，用于核销取货" />
        </el-form-item>
        <el-form-item label="自提状态" prop="pickupStatus">
          <el-select v-model="form.pickupStatus" placeholder="请选择自提状态">
            <el-option
              v-for="dict in pickup_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="实际提货时间" prop="pickupTime">
          <el-date-picker clearable
            v-model="form.pickupTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择实际提货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="自提备注信息" prop="pickupNotes">
          <el-input v-model="form.pickupNotes" placeholder="请输入自提备注信息" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Shoporder">
import { listShoporder, getShoporder, delShoporder, addShoporder, updateShoporder } from "@/api/transaction/shoporder"

const { proxy } = getCurrentInstance()
const { pickup_status, order_status, delivery_method, pay, del_flag } = proxy.useDict('pickup_status', 'order_status', 'delivery_method', 'pay', 'del_flag')

const shoporderList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: null,
    membersId: null,
    storeId: null,
    address: null,
    couponId: null,
    itemsAmount: null,
    discountAmount: null,
    couponAmount: null,
    shippingFee: null,
    totalAmount: null,
    status: null,
    paymentType: null,
    paymentTime: null,
    expectDeliveryTime: null,
    actualDeliveryTime: null,
    notes: null,
    deliveryMethod: null,
    pickupCode: null,
    pickupStatus: null,
    pickupTime: null,
    pickupNotes: null
  },
  rules: {
    orderNo: [
      { required: true, message: "订单编号，唯一不能为空", trigger: "blur" }
    ],
    membersId: [
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    storeId: [
      { required: true, message: "门店ID不能为空", trigger: "blur" }
    ],
    itemsAmount: [
      { required: true, message: "商品总金额不能为空", trigger: "blur" }
    ],
    totalAmount: [
      { required: true, message: "应付款总金额不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询订单表列表 */
function getList() {
  loading.value = true
  listShoporder(queryParams.value).then(response => {
    shoporderList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    orderNo: null,
    membersId: null,
    storeId: null,
    address: null,
    couponId: null,
    itemsAmount: null,
    discountAmount: null,
    couponAmount: null,
    shippingFee: null,
    totalAmount: null,
    status: null,
    paymentType: null,
    paymentTime: null,
    expectDeliveryTime: null,
    actualDeliveryTime: null,
    notes: null,
    delFlag: null,
    createTime: null,
    updateTime: null,
    deliveryMethod: null,
    pickupCode: null,
    pickupStatus: null,
    pickupTime: null,
    pickupNotes: null
  }
  proxy.resetForm("shoporderRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加订单表"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getShoporder(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改订单表"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["shoporderRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateShoporder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addShoporder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

// /** 删除按钮操作 */
// function handleDelete(row) {
//   const _ids = row.id || ids.value
//   proxy.$modal.confirm('是否确认删除订单表编号为"' + _ids + '"的数据项？').then(function() {
//     return delShoporder(_ids)
//   }).then(() => {
//     getList()
//     proxy.$modal.msgSuccess("删除成功")
//   }).catch(() => {})
// }

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认逻辑删除订单表编号为"' + _ids + '"的数据项？').then(function() {
    return delShoporder(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('transaction/shoporder/export', {
    ...queryParams.value
  }, `shoporder_${new Date().getTime()}.xlsx`)
}

getList()
</script>
